package cn.lisens.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.lisens.filter.CustomFilter;
import cn.lisens.interceptor.LoggerInterceptor;

@Configuration
public class WebConfig implements WebMvcConfigurer {

	@Autowired
	private HandlerInterceptor customInterceptor;
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(customInterceptor)
				.addPathPatterns("/**")
				.excludePathPatterns("/static/**", "/error");
		registry.addInterceptor(new LoggerInterceptor())
				.addPathPatterns("/**")
				.excludePathPatterns("/static/**", "/error");
	}
	
	@Bean
    public FilterRegistrationBean<CustomFilter> customFilterRegistration() {
        FilterRegistrationBean<CustomFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CustomFilter());
        registration.addUrlPatterns("/*"); // 过滤所有请求
        registration.setOrder(1); // 设置顺序
        return registration;
    }
}
